﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using DAL;

namespace Pay1
{
    public partial class Form1 : Form
    {
        public static string IDValue { get; private set; }
        public Form1()
        {
            Login lg = new Login();
            if (lg.ShowDialog() == DialogResult.OK)
            {
                InitializeComponent();
                GridViewBind(); //现金列表  
                GridViewInit();
                SetBalance();
                GetMoneyTip();
                GridViewCardBind(); //信用卡列表

                lg.Dispose();
            }
        }
        public void GridViewInit()
        {
            int i;
            dataGridView1.Columns[0].FillWeight = 4;
            for (i = 1; i < 9; i++)
            {
                dataGridView1.Columns[i].FillWeight = 10;
            }
            dataGridView1.Columns[i].FillWeight = 16;
        }

        public void SetBalance()//计算余额
        {
            for (int i = 1; i < dataGridView1.Rows.Count+1; i++ )
                dataGridView1.Rows[i-1].Cells["余额"].Value = PaySpend.GetSpendBalance(i);
        }
        public string GetMoneyTip()//统计日均消费 单数信息
        {
            int linecount = PaySpend.SpendCount();
            string[] para = PaySpend.GetSpendBalance();
            labeltip.Text = string.Format("共{0}条记录。总计支出金额{1}, 平均每日消费金额为{2}元", linecount, para[0], para[1]);

            return labeltip.Text;
        }

        #region DataBind
        public DataTable GridViewBind()
        {
            DataTable dt = PaySpend.GetSpendList();
            this.dataGridView1.DataSource = dt;
            return dt;
        }
        public DataTable GridViewCardBind()
        {
            DataTable dt = PaySpend.GetCardList();
            this.dataGridView2.DataSource = dt;
            return dt;
        }
        #endregion

        string strtime = "登录时间：" + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
        private void timer1_Tick(object sender, EventArgs e)
        {
            this.toolStripStatusLabel3.Text = strtime;
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            this.toolStripStatusLabel3.Text = strtime;
            this.timer1.Interval = 1000;
            this.timer1.Start();
        }

        #region 增-删-改 functions
        public void AddItem()
        {
            Add add = new Add();
            if (add.ShowDialog() == DialogResult.OK)
            {
                GridViewBind();
                SetBalance();
            }
            add.Dispose();
        }
        public void DelItem()
        {
            int count = dataGridView1.SelectedRows.Count;
            if (count != 0)
            {
                DialogResult result = MessageBox.Show("确定删除记录？", "删除记录", MessageBoxButtons.OKCancel);
                if (result == DialogResult.OK)
                {
                    string id = "";
                    while (count > 0)
                    {
                        string tmp = dataGridView1.SelectedRows[count - 1].Cells[1].Value.ToString();
                        tmp = string.Format("'{0}'", tmp);
                        id += tmp + ",";
                        dataGridView1.Rows.RemoveAt(dataGridView1.SelectedRows[count - 1].Index);
                        count--;
                    }
                    id = id.Substring(0, id.Length - 1);
                    int s = PaySpend.Del(id);
                    if (s != 0)
                        MessageBox.Show(string.Format("您选择的{0}条记录已成功删除.", s));
                }
            }
            else
                MessageBox.Show("亲，请先选择想要删除的记录哦", "删除记录", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            SetBalance();
        }
        public void EditItems()
        {
            int i = dataGridView1.CurrentRow.Index;
            DataTable dt = PaySpend.GetList();
            IDValue = dt.Rows[i][0].ToString();

            Add update = new Add();
            if (update.ShowDialog() == DialogResult.OK)
            {
                IDValue = null;
                GridViewBind();
                SetBalance();
                update.Dispose();
            }
        }
        #endregion

        //序号列
        private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            foreach (DataGridViewRow dr in dataGridView1.Rows)
                dr.Cells[0].Value = dr.Index + 1;
        }

        //ToolTip 提示
        public string GetID(int index)
        {
            if (this.dataGridView1.Rows[index].Cells[1].Value!= null)
                return this.dataGridView1.Rows[index].Cells[1].Value.ToString();
            else
                return "";
        }
        private void dataGridView1_CellToolTipTextNeeded(object sender, DataGridViewCellToolTipTextNeededEventArgs e)
        {
            string ID = "";
            if (e.RowIndex != -1)
                ID = GetID(e.RowIndex);
            if (ID != "")
            {
                DataTable dt = PayDetail.GetDetailList(ID);
                string str = "";
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    str += Convert.ToString(i+1) + "    ";
                    for (int j = 0; j < dt.Columns.Count; j++)
                    {
                        str += dt.Rows[i][j].ToString() + "    ";
                    }
                    str += "\n";
                }
                e.ToolTipText = str;
            }
            else
                e.ToolTipText = "";
        }

        //Edit
        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            EditItems();
        }
        private void toolStriptoolStripEdit_Click(object sender, EventArgs e)
        {
            EditItems();
        }


        //------未完成----------------------------------------------------------------
        public void SaveItem()
        {
            MessageBox.Show("这是一个保存按钮功能测试。");
        }
        private void 测试ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            test t = new test();
            t.ShowDialog();
            t.Dispose();
        }

        private void 编号格式ToolStripMenuItem_Click(object sender, EventArgs e)
        {
//             MessageBox.Show("确定修改编码格式yyyyMMdd-XX为XXyyyyMMdd吗？");
//             DataTable dt = DAL.PayDetail.GetList("");
// 
//             MessageBox.Show(dt.Rows[0][1].ToString());
        }

        private void toolStripLabelAdd_Click(object sender, EventArgs e)
        {
            IDValue = null;
            this.AddItem();
        }

        private void toolStripLabel1About_Click(object sender, EventArgs e)
        {
            AboutBox1 about = new AboutBox1();
            about.ShowDialog();
            about.StartPosition = FormStartPosition.CenterParent;
            about.Dispose();
        }

        private void toolStripLabelSave_Click(object sender, EventArgs e)
        {
            this.SaveItem();
        }

        private void toolStripLabelDel_Click(object sender, EventArgs e)
        {
            this.DelItem();
        }

        private void toolStripLabelFind_Click(object sender, EventArgs e)
        {
            MessageBox.Show("亲, 查询模块正在开发，敬请期待哦", "查询记录", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
        }

        private void toolStripLabelRefresh_Click(object sender, EventArgs e)
        {
            GridViewBind();                   
            GetMoneyTip();
            MessageBox.Show("亲, 列表重新加载已成功", "记录刷新", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
        }
        //----------------------------------------------------------------------------
    }
}
